package chase.myrewards;

import java.io.*;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.logging.Logger;

import javax.servlet.*;
import javax.servlet.http.*;

import com.google.appengine.api.rdbms.AppEngineDriver;
import com.google.appengine.api.utils.SystemProperty;
import chase.myrewards.beans.*;

/**
 * Servlet that takes customer id and account number as input and gets 
 * the breakdown of recently earned rewards and puts it in a bean.
 * 
 * @author Meena Abdou
 * @date April 24, 2013
 */

public class RecentlyAccruedServlet extends HttpServlet implements Serializable {

	/**
	 * This number must be unique for every class. And it can never change.
	 * You have to generate it automatically by clicking on the class name and
	 * then hitting Ctrl+1.  Then click on Add generated serial version ID. Then
	 * copy these comments on top of the generated id.
	 * 
	 * @serial serialVersionUID
	 */
	private static final long serialVersionUID = 8901367918111878029L;
	private static final Logger log = Logger.getLogger(AccountListServlet.class.getName());
	private Customer cust;
	private Account account;
	private RecentlyAccruedBean ra;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		log.info("Session Id is " + request.getSession().getId());
		
		HttpSession session = request.getSession();
		String address;
		address = "/WEB-INF/jsp/recentyaccrued.jsp";
		cust = null;
		account = null;
		ra = new RecentlyAccruedBean();
		
		if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
			java.sql.Connection c = null;
			try {
				DriverManager.registerDriver(new AppEngineDriver());
				c = DriverManager.getConnection("jdbc:google:rdbms://myrewards2013:myrewardsdatabase/myrewards");
								
				cust = (Customer) session.getAttribute("customer");
				account = (Account) request.getSession().getAttribute("accountinfo");
				String id = cust.getCustomerID();
				String num = account.getAccountNum();
				// Getting all program ids for the selected account number			
				java.sql.Statement stmt1 =  c.createStatement();
				java.sql.ResultSet rs1 = stmt1.executeQuery(Queries.PROGRAM_ID_BALANCES(num, id));
				rs1.next();
				
				
				do
				{
				// Getting all recently accrued points and description for each program id
					java.sql.Statement stmt2 =  c.createStatement();	
					java.sql.ResultSet rs2 = stmt2.executeQuery(Queries.RECENTLY_ACCRUED(rs1.getString("program_id"),num));
					rs2.next();	
					do {
						// Loading program description and points 
						ra.insert(rs2.getString("program.description"), rs2.getString("balances.recently_accrued"));
												
					} while(rs2.next());// End of rs2 while
				}while(rs1.next());// End of rs1 while

				int prev = account.getPreviousBalance();
				int recent = account.getRecentlyAccrued(); 
				int used = account.getUsedPoints();	
				int currbalance;
				currbalance = (prev-used) + recent;
				ra.setCurrentBalance(currbalance);
				
				session.setAttribute("recentlyaccrued", ra);

				RequestDispatcher dispatcher = request.getRequestDispatcher(address);
				dispatcher.forward(request, response);
							
			} 
			catch (SQLException e) {
				e.printStackTrace();
			} 
			finally {
				if (c != null){ 
					try {
						c.close();
					} 
					catch (SQLException ignore) {}
				}
			} // End of try-catch block
		}

		else{
		
			session.setAttribute("recentlyaccrued", ra);

			RequestDispatcher dispatcher = request.getRequestDispatcher(address);
			dispatcher.forward(request, response);
		}
		
		return;
	}
}
